﻿using System;
using System.Linq;
using GdHotel.Data.Interfaces;

namespace GdHotel.Data.Repositories
{
    public class NewsRepository : INewsRepository
    {
        private GdHotelEntities entities = new GdHotelEntities();

        public IQueryable<News> FindAll()
        {
            return entities.News.AsQueryable();
        }

        public IQueryable<News> FindAllByLang(Language lang)
        {
            return entities.News.AsQueryable().Where(n => n.LanguageId == lang.Id);
        }

        public News GetById(Int32 id)
        {
            return entities.News.SingleOrDefault(n => n.Id == id);
        }

        public void Insert(News n)
        {
            entities.AddToNews(n);
        }

        public void Delete(News n)
        {
            entities.News.DeleteObject(n);
        }

        public void Save()
        {
            entities.SaveChanges();
        }

        public void Dispose()
        {
            entities.Dispose();
        }

        public IQueryable<News> GetLatestNewsByLang(Language language)
        {
            return entities.News.AsQueryable().Where(n => n.LanguageId == language.Id).OrderByDescending(n => n.PublicationDate);
        }

        public IQueryable<News> GetLatestNewsByLang(Language language, Int32 count)
        {
            return GetLatestNewsByLang(language).Take(count);
        }
    }
}
